package cn.didi.feidi.javabackend.redis.controller;

import cn.didi.feidi.javabackend.mysql.jpa.entity.Userxx;
import cn.didi.feidi.javabackend.mysql.jpa.user.User;
import cn.didi.feidi.javabackend.redis.cacheKey.UserCacheKey;
import cn.didi.feidi.javabackend.redis.cacheKey.UserxxCacheKey;
import cn.didi.feidi.javabackend.redis.repository.UserRedRep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * Created by Administrator on 2017/7/7.
 */
@RestController
public class UserController {
    @Autowired
    UserRedRep userRedRep;

    /**
     * 例子，访问http://localhost:8080/userol/example
     * @return {"id":2,"name":"xiaoming","createdate":1499345500000,"roles":[{"id":2,"name":"admin"}]}
     */
    @RequestMapping("/getUserxx")
    Userxx get(@RequestParam Long id) {
        Userxx userxx = userRedRep.get(UserxxCacheKey.USER_+id);
        return userxx;
    }

    /**
     * 例子，访问http://localhost:8080/userol/list
     * @return [{"id":2,"name":"xiaoming","createdate":1499345500000,"roles":[{"id":2,"name":"admin"}]},{"id":3,"name":"陈鸿","createdate":1499345515000,"roles":[]}]
     */
    @RequestMapping("/userxx/post")
    void add(Userxx userxx) {
//        Map<String, Integer> map = new HashMap<String, Integer>();
//        map.put("string1",1);
//        map.put("string2",2);
//        user.setMap(map);
//        List<Role> list = new ArrayList<Role>();
//        Role role = new Role();
//        role.setId((long)1);
//        role.setName("role1");
//        Role role2 = new Role();
//        role2.setId((long)2);
//        role2.setName("role2");
//        list.add(role);
//        list.add(role2);
//        user.setRoles(list);
//        List<String> list2 = new ArrayList<String>();
//        list2.add("str1");
//        list2.add("str2");
//        user.setList2(list2);
        userRedRep.add(UserxxCacheKey.USER_+userxx.getId(),1000L,userxx);
    }

    @RequestMapping("/addUserxx")
    @ResponseBody
    String addUserxx(Userxx userxx) throws IOException {
//        userxxRedRep.add(UserxxCacheKey.USER_+userxx.getId(),60L, userxx);
//        int len = request.getContentLength();
//        InputStream inputStream = request.getInputStream();
//        byte[] buffer = new byte[len];
//        inputStream.read(buffer, 0, len);
//        String GsonUserxx = new String(buffer);
//        System.out.println(GsonUserxx);
//        Userxx userxx = (Userxx)JSONObject.toBean(JSONObject.fromObject(GsonUserxx), Userxx.class);
//        System.out.println("redis-UerxxController catch userxx. " + userxx.getName());
        userRedRep.add(UserxxCacheKey.USER_+userxx.getId(),60L, userxx);
        return "OK";
    }

    @RequestMapping("/getUserById")
    public User getUserById(@RequestParam String id){
        return userRedRep.getUserById(UserCacheKey.USER_ + id);
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(@RequestBody User user){
        userRedRep.addUser(UserCacheKey.USER_ + user.getUserId(), 60L, user);
        return "ok";
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(@RequestBody User user){
        if(user != null){
            userRedRep.deleteUserById(UserCacheKey.USER_ + user.getUserId());
            userRedRep.addUser(UserCacheKey.USER_ + user.getUserId(), 60L, user);
        }
        return "OK";
    }

    @RequestMapping("/deleteUserById")
    @ResponseBody
    public String deleteUserById(@RequestParam String id){
        userRedRep.deleteUserById(id);
        return "OK";
    }
}
